package strings;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.mindview.util.TextFile;

import org.junit.Ignore;
import org.junit.Test;

public class JGrepMY {

	@Test
	@Ignore
	public void test0011() {
		List<String> list = new ArrayList<String>();
		//JGrep.java "\\b[Ssct]\\w+"
		list.add("C:/wxg/git3/thinkinginjava/thinkinginjava/src/main/java/strings/JGrep.java");
		list.add("\\b[Ssct]\\w+");
		aa(list.toArray(new String[]{}));
	}
	
	@Test
	//@Ignore
	public void test001() {
		List<String> list = new ArrayList<String>();
		/**
		 * 
path: /C:/wxg/git3/thinkinginjava/thinkinginjava/src/main/java/strings/JGrep.java
	   C:\wxg\git3\thinkinginjava\thinkinginjava\src\main\java\strings\JGrep.java
		 */
//		String path = getJavaFilePath() + "JGrep.java";
//		System.out.println(String.format("path: %s", path));
//		path = "C:/wxg/git3/thinkinginjava/thinkinginjava/src/main/java/strings/JGrep.java";
		//list.add( path );
		list.add("C:/wxg/git3/thinkinginjava/thinkinginjava/src/main/java/strings/JGrep.java");
		list.add("//b[Ssct]//w+");
		aa(list.toArray(new String[]{}));
	}

	private static void aa(String[] args) {
		if (args.length < 2) {
			System.out.println("Usage: java JGrep file regex");
			System.exit(0);
		}
		System.out.println(String.format("args[0]: %s", args[0]));
		System.out.println(String.format("args[1]: %s", args[1]));
		Pattern p = Pattern.compile(args[1]);
		// Iterate through the lines of the input file:
		int index = 0;
		Matcher m = p.matcher("");
		for (String line : new TextFile(args[0])) {
			m.reset(line);
			while (m.find())
			{
				System.out.println(index++ + ": " + m.group() + ": "
						+ m.start());
			}
		}
	}
	
	private static String getJavaFilePath() {
		StringBuilder sbb = new StringBuilder();
		String rootpath = JGrepMY.class.getClassLoader().getResource("").getPath();
		String pkg = JGrepMY.class.getPackage().getName();
		//rootpath = rootpath.replaceAll("target[/\\\\]test-classes[/\\\\]", "");
		rootpath = rootpath.replaceAll("\\\\", "/");
		rootpath = rootpath.replaceFirst("/", "");
		rootpath = rootpath.replaceAll("target/test-classes/", "");
		sbb.append(rootpath).append("src/main/java/")//.append("src/test/java/")
			.append(pkg).append("/");
		rootpath = sbb.toString();
		return rootpath;
	}
	
	@Test
	@Ignore
	public void test004() {
		System.out.println( String.format("rootpath: %s", getJavaFilePath()) );
	}
	
	@Test
	@Ignore
	public void test002() {
		String path = JGrepMY.class.getResource("").getPath();
		System.out.println( String.format("path: %s", path) );
		String rootpath = JGrepMY.class.getClassLoader().getResource("").getPath();
		System.out.println( String.format("rootpath: %s", rootpath) );
		/**
		 * 
C:/wxg/git3/thinkinginjava/thinkinginjava/src/test/java/strings/JGrepMY.java
		 */
		String pkg = JGrepMY.class.getPackage().getName();
		System.out.println( String.format("pkg: %s", pkg) );
		
		System.out.println(String.format("File.separator: %s", File.separator));
		System.out.println( File.separatorChar );
		
		//rootpath = rootpath.replaceAll("target[/\\\\]test-classes[/\\\\]", "");
		rootpath = rootpath.replaceAll("\\\\", "/");
		rootpath = rootpath.replaceAll("target/test-classes/", "");
		rootpath = rootpath + "src/test/java/" + pkg + "/";
		System.out.println( String.format("rootpath: %s", rootpath) );
	}
	
	@Test
	@Ignore
	public void test003() {
		String strA = null;
		String strB = "C:/wxg/git3/";
		BufferedReader br = null;
		String filepath = JGrepMY.class.getResource("JGrepMY.txt").getPath();
		try {
			br = new BufferedReader(new FileReader(new File(filepath)));
			strA = br.readLine();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != br)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println(String.format("strA: %s", strA));
		System.out.println("-----------");
		System.out.println( strA.replaceAll("[/\\\\]", "-") );
		System.out.println( strB.replaceAll("[/\\\\]", "-") );
	}
	
}
